package com.milling;

import com.milling.common.utils.modbus.ModbusMasterPoolFactory;
import com.milling.common.utils.modbus.ModbusTcpService;
import com.milling.common.utils.modbus.ModbusTcpServiceImpl;
import com.serotonin.modbus4j.ModbusMaster;
import org.apache.commons.pool2.ObjectPool;

public class ModbusTcpTest {

    public static void main(String[] args) {
        // 从参数中读取IP、端口和连接池大小
        String host = "192.168.237.88"; // 从配置文件或命令行参数中读取
        int port = 502; // 从配置文件或命令行参数中读取
        int poolSize = 5; // 从配置文件或命令行参数中读取

        // 创建连接池
        ObjectPool<ModbusMaster> modbusMasterPool = ModbusMasterPoolFactory.createPool(host, port, poolSize);

        // 创建ModbusService实例
        ModbusTcpService modbusTcpService = new ModbusTcpServiceImpl(modbusMasterPool);

        try {
            // 示例：读取单个保持寄存器
            int slaveId = 1;
//            int registerAddress = 5900;
            int registerAddress = 5000;
            /*short[] values = new short[100];
            Arrays.fill(values, (short)0);
            for (int i = 5000; i < 5800;) {
                modbusTcpService.writeHoldingRegisters(slaveId, registerAddress, values);
                System.out.println("writeHoldingRegisters " + registerAddress + ": " + values);
                registerAddress += 100;
                i = registerAddress;
            }*/
            /*for (int i = 0; i < 800; i++) {
                modbusTcpService.writeHoldingRegister(slaveId, 5000+i, 9);
                System.out.println("writeHoldingRegister " + 5000+i + ": " + 9);
            }*/

//            boolean value = modbusTcpService.readMXBit(slaveId, 5000, 1);
            boolean[] values = modbusTcpService.readCoils(slaveId, 5000, 800);
            for (int i = 0; i < values.length; i++) {
                System.out.println("readHoldingRegister " + (i+registerAddress) + ": " + values[i]);
            }
            // 示例：写入单个保持寄存器
/*
            modbusTcpService.writeHoldingRegister(slaveId, 5020, 0);
            modbusTcpService.writeHoldingRegister(slaveId, 5023, 0);
            modbusTcpService.writeHoldingRegister(slaveId, registerAddress, 1);

            short value = modbusTcpService.readHoldingRegister(slaveId, registerAddress);
            System.out.println("readHoldingRegister " + registerAddress + ": " + value);
*/
//            for (int j = 0; j < 100; j++) {
            // 创建连接池
//                ObjectPool<ModbusMaster> modbusMasterPool = ModbusMasterPoolFactory.createPool(host, port, poolSize);
//
//                // 创建ModbusService实例
//                ModbusTcpService modbusTcpService = new ModbusTcpServiceImpl(modbusMasterPool);

/*
                try {

                    short[] values2 = modbusTcpService.readHoldingRegisters(slaveId, registerAddress, 100);
                    for (int i = 0; i < values2.length; i++) {
                        System.out.println("readHoldingRegisters " + i + ": " + values2[i]);
                    }
                    System.out.println("\r\n");
                    System.out.println("\r\n");
                    System.out.println("\r\n");
//                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // 关闭连接池
                    modbusMasterPool.close();
                }
*/
//            }

            // 示例：读取多个保持寄存器
            /*short[] values = modbusTcpService.readHoldingRegisters(slaveId, 500, 10);
            for (int i = 0; i < values.length; i++) {
                System.out.println("readHoldingRegisters " + i + ": " + values[i]);
            }*/

            // 示例：写入多个保持寄存器
//            short[] newValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//            modbusTcpService.writeHoldingRegisters(slaveId, 5130, newValues);

//            short[] newValues = {1, 2, 3, 4, 5};
//            modbusTcpService.writeHoldingRegisters(slaveId, 0, newValues);

            // 示例：读取输入寄存器
//            short[] inputValues = modbusTcpService.readInputRegisters(slaveId, 0, 10);
//            for (int i = 0; i < inputValues.length; i++) {
//                System.out.println("readInputRegisters " + i + ": " + inputValues[i]);
//            }

            // 示例：读取线圈状态
/*            boolean[] coils = modbusTcpService.readCoils(slaveId, 8192, 1);
            for (int i = 0; i < coils.length; i++) {
                System.out.println("readCoils " + i + ": " + coils[i]);
            }

//             示例：写入单个线圈
            modbusTcpService.writeCoil(slaveId, 8192, true);*/

            // 示例：写入多个线圈
//            boolean[] newCoils = {true, false, true, false, true};
//            modbusTcpService.writeCoils(slaveId, 0, newCoils);


            /*ModbusFactory modbusFactory = new ModbusFactory();
            ModbusMaster modbusMaster = modbusFactory.createTcpMaster("192.168.1.100", false); // 第二个参数是 useComPortProperties，通常设置为 false
            modbusMaster.init(); // 初始化连接

            // 创建定位器，用于读取保持寄存器中的 M100 地址
            int reference = 0; // MODBUS 保持寄存器的起始地址，对应 M100
            int length = 1; // 读取的寄存器数量
            BaseLocator locator = BaseLocator.holdingRegister(slaveId, reference, length);

            // 读取寄存器的值并进行类型转换
            Object valueObject = modbusMaster.getValue(locator);
            if (valueObject instanceof Integer) {
                int value2 = (Integer) valueObject;
                System.out.println("M100 的值: " + value2);
            } else {
                System.out.println("读取的值类型不正确");
            }

            // 写入值到寄存器
            modbusMaster.setValue(locator, 1); // 将 M100 设为 1（ON）

            modbusMaster.destroy(); // 关闭连接*/

/*
            ModbusFactory modbusFactory = new ModbusFactory();
            ModbusMaster modbusMaster = modbusFactory.createTcpMaster("192.168.1.1", 502);
            modbusMaster.init(); // 初始化连接

            ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(1, 99, 1);
            ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) modbusMaster.send(request);

            if (response.isException()) {
                System.out.println("Exception response: message=" + response.getExceptionMessage());
            } else {
                short[] data = response.getShortData();
                System.out.println("M100 的值: " + data[0]);
            }
            modbusMaster.destroy(); // 关闭连接
*/
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接池
            modbusMasterPool.close();
        }
    }
    public static void mainPZ(String[] args) {
        // 从参数中读取IP、端口和连接池大小
        String host = "192.168.237.88"; // 从配置文件或命令行参数中读取
        int port = 502; // 从配置文件或命令行参数中读取
        int poolSize = 5; // 从配置文件或命令行参数中读取

        // 创建连接池
        ObjectPool<ModbusMaster> modbusMasterPool = ModbusMasterPoolFactory.createPool(host, port, poolSize);

        // 创建ModbusService实例
        ModbusTcpService modbusTcpService = new ModbusTcpServiceImpl(modbusMasterPool);

        try {
            // 示例：读取单个保持寄存器
            int slaveId = 1;
//            int registerAddress = 5000;
            int registerAddress = 570;
            /*short[] values = new short[100];
            Arrays.fill(values, (short)0);
            for (int i = 5000; i < 5800;) {
                modbusTcpService.writeHoldingRegisters(slaveId, registerAddress, values);
                System.out.println("writeHoldingRegisters " + registerAddress + ": " + values);
                registerAddress += 100;
                i = registerAddress;
            }*/
            /*for (int i = 0; i < 800; i++) {
                modbusTcpService.writeHoldingRegister(slaveId, 5000+i, 9);
                System.out.println("writeHoldingRegister " + 5000+i + ": " + 9);
            }*/

            short value = modbusTcpService.readHoldingRegister(slaveId, registerAddress);
            System.out.println("readHoldingRegister " + registerAddress + ": " + value);

            // 示例：写入单个保持寄存器
            modbusTcpService.writeHoldingRegister(slaveId, registerAddress, 0);


            value = modbusTcpService.readHoldingRegister(slaveId, registerAddress);
            System.out.println("readHoldingRegister " + registerAddress + ": " + value);

/*            short[] values2 = modbusTcpService.readHoldingRegisters(slaveId, 5000, 100);
            for (int i = 0; i < values2.length; i++) {
                System.out.println("readHoldingRegisters " + i + ": " + values2[i]);
            }*/

            // 示例：读取多个保持寄存器
            /*short[] values = modbusTcpService.readHoldingRegisters(slaveId, 500, 10);
            for (int i = 0; i < values.length; i++) {
                System.out.println("readHoldingRegisters " + i + ": " + values[i]);
            }*/

            // 示例：写入多个保持寄存器
//            short[] newValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//            modbusTcpService.writeHoldingRegisters(slaveId, 5130, newValues);

//            short[] newValues = {1, 2, 3, 4, 5};
//            modbusTcpService.writeHoldingRegisters(slaveId, 0, newValues);

            // 示例：读取输入寄存器
//            short[] inputValues = modbusTcpService.readInputRegisters(slaveId, 0, 10);
//            for (int i = 0; i < inputValues.length; i++) {
//                System.out.println("readInputRegisters " + i + ": " + inputValues[i]);
//            }

            // 示例：读取线圈状态
//            boolean[] coils = modbusTcpService.readCoils(slaveId, 0, 10);
//            for (int i = 0; i < coils.length; i++) {
//                System.out.println("readCoils " + i + ": " + coils[i]);
//            }

            // 示例：写入单个线圈
//            modbusTcpService.writeCoil(slaveId, 0, true);

            // 示例：写入多个线圈
//            boolean[] newCoils = {true, false, true, false, true};
//            modbusTcpService.writeCoils(slaveId, 0, newCoils);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接池
            modbusMasterPool.close();
        }
    }
}
